package com.hc.controller;

import com.hc.common.bean.PageBean;
import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.constant.UserConst;
import com.hc.domain.Collect;
import com.hc.domain.User;
import com.hc.res.SkuItem;
import com.hc.service.CollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * @author hecai
 * @date 2021/12/23 14:41
 */
@Slf4j
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Resource
    private CollectService collectService;

    @PostMapping
    public Result create(@RequestBody Collect collect) {
        boolean res = collectService.save(collect);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result updateById(@RequestBody Collect collect) {
        boolean res = collectService.updateById(collect);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Serializable id) {
        boolean res = collectService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Serializable id) {
        Collect res = collectService.getById(id);
        return ResultUtil.success("查询成功").setData(res);
    }

    //@PostMapping
    //public Result getByCondition(@RequestBody CollectQuery condition) {
    //    List<Collect> list = collectService.queryByCondition(condition);
    //    return ResultUtil.success("查询成功").setData(list);
    //}

    /**
     * 加入收藏
     *
     * @param collect
     * @return
     */
    @PostMapping("/add")
    public Result addCollect(@RequestBody Collect collect) {
        boolean saveRes = collectService.save(collect);
        if (saveRes) {
            return ResultUtil.success("收藏成功");
        } else {
            return ResultUtil.fail("收藏失败");
        }
    }

    /**
     * 收藏列表
     */
    @GetMapping("/list")
    public Result listCollectByUser(Integer pageNum, Integer pageSize) {
        User user = UserConst.user;
        PageBean<SkuItem> skuItemPageBean = collectService.getSkuItemPageBean(pageNum, pageSize, user.getId());

        return ResultUtil.success().setData(skuItemPageBean);
    }


}